import BaseController from "../../cfw/mvc/BaseController";
import ModuleManager, { ModuleID } from "../../cfw/module/ModuleManager";
import { UIIndex } from "../../cfw/tools/Define";
import LobbyController from "../lobby/LobbyController";
import SmallGameManager from "./SmallGameManager";
import SDKManager from "../../sdk/SDKManager";
import TextureInfo from "../../engine/comps/TextureInfo";
import { fillColorName, paintColorName, SceneName } from "../../public/Config";
import User from "../db/User";
import SceneManager from "../../engine/SceneManager";
import { ResType } from "../../cfw/res/ResInterface";
import ResItem from "../../cfw/res/ResItem";
import { SDKEventName } from "../../sdk/SDKConfig";
import RedPacketC from "../../sdk/redpacket/RedPacketC";
import FloatC from "../float/FloatC";


export default class SmallGameController extends BaseController {

    private static ins: SmallGameController;

    static instance() {
        if (!this.ins) {
            this.ins = new SmallGameController()
        }
        return this.ins;
    }

    private fillColorIndex: number = 0;

    getFillColorIndex() {
        return this.fillColorIndex;
    }

    intoLayer() {
        this.pushView('prefabs/SmallGameView', "SmallGameView", SmallGameManager.instance(), ModuleManager.getLoader(), UIIndex.ROOT)
    }

    gotoLobby() {
        LobbyController.instance().intoLayer()
    }


    back(draw) {
        if (SDKManager.getChannel().cantUseImageDataURL()) {
            let tex: TextureInfo = draw;
            tex.writeFile(fillColorName + this.fillColorIndex + '.txt')
        } else {
            cc.log('fillColorName + this.fillColorIndex ', fillColorName + this.fillColorIndex)
            User.instance().setImageData(fillColorName + this.fillColorIndex, draw)
        }
        this.gotoLobbyScene();
    }

    gotoLobbyScene() {
        ModuleManager.setModuleID(ModuleID.LOBBY)
        ModuleManager.getLoader().releaseRes('Scene/Lobby',ResType.Scene)
        ModuleManager.getLoader().loadRes('Scene/Lobby', ResType.Scene, (err, item: ResItem) => {
            if (err) {
                return;
            }
            SceneManager.instance().runScene(item.getRes())
            LobbyController.instance().intoLayer()
            FloatC.instance().intoLayer()
            RedPacketC.instance().showFriend()
        })

        // SceneManager.instance().loadScene(SceneName.LOBBY, () => {
        //     LobbyController.instance().intoLayer()
        // })

    }



    intoFillColorView(index: number) {
        this.fillColorIndex = index;
        // SceneManager.instance().loadScene(SceneName.FILL_COLOR, () => {
        //     this.pushView('prefabs/FillColorRoomView', 'FillColorRoomView', new SmallGameManager(), ModuleManager.getLoader(), UIIndex.ROOT)
        // })
        ModuleManager.setModuleID(ModuleID.SMALL_GAME)
        // ModuleManager.getLoader().releaseRes('Scene/FillColor',ResType.Scene)
        ModuleManager.getLoader().loadRes('Scene/FillColor', ResType.Scene, (err, item: ResItem) => {
            if (err) {
                cc.error('intoFillColorView err ', err)
                return;
            }
            SceneManager.instance().runScene(item.getRes())
            this.pushView('prefabs/FillColorRoomView', 'FillColorRoomView', new SmallGameManager(), ModuleManager.getLoader(), UIIndex.ROOT)
            
        })
        SDKManager.getChannel().sendEvent(SDKEventName.smallgame,index)
        // SceneManager.instance().runScene(SceneName.FILL_COLOR, ModuleManager.getLoader(), (err, scene: cc.SceneAsset) => {
        //     this.pushView('prefabs/FillColorRoomView', 'FillColorRoomView', new SmallGameManager(), ModuleManager.getLoader(), UIIndex.ROOT)
        // })
    }

    intoPaintColorView() {
        this.pushView('prefabs/PaintColorView', 'PaintColorView', new SmallGameManager(), ModuleManager.getLoader(), UIIndex.ROOT)
    }

}
